package com.blyat.xsoft.gateway.restful.service.impl;

import com.blyat.xsoft.gateway.restful.entity.Server;
import com.blyat.xsoft.gateway.restful.mapper.ServerMapper;
import com.blyat.xsoft.gateway.restful.service.ServerService;
import com.blyat.xsoft.gateway.ribbon.loadbalancer.server.ServerLoader;
import com.blyat.xsoft.kernel.restful.service.CrudService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Auther: syh
 * @Date: 2020/9/14
 * @Description:
 */
@Service
public class ServerServiceImpl extends CrudService<Server, ServerMapper> implements ServerService, ServerLoader<Server> {
    @Override
    public List<Server> getServerByClient(String client) throws Exception {
        List<Server> servers = mapper.getServerByClient(client);
        return Optional.ofNullable(servers).orElseGet(() -> new ArrayList<>()).stream()
                .map(server -> {
                    server.setId(
                            String.format("%s://%s:%s", server.getSchema(), server.getHost(), server.getPort())
                    );
                    return server;
                }).collect(Collectors.toList());
    }
}
